package com.yuqing.sentimentapp.workspace.typhoon;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.typhoon.entity.TyphoonInfoResult;
import com.yuqing.sentimentapp.workspace.typhoon.entity.TyphoonReportResult;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 台风抢险
 * Created by shenhe on 2020/4/3.
 */
public class TyphoonFillInActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    @BindView(R.id.et_clearBillboardCount)
    EditText mEtClearBillboardCount;
    @BindView(R.id.et_trafficAccidentCount)
    EditText mEtTrafficAccidentCount;
    @BindView(R.id.et_clearCollapseCount)
    EditText mEtClearCollapseCount;
    @BindView(R.id.et_homeTrappedCount)
    EditText mEtHomeTrappedCount;
    @BindView(R.id.et_buildCollapseCount)
    EditText mEtBuildCollapseCount;
    @BindView(R.id.et_drainageCount)
    EditText mEtDrainageCount;
    @BindView(R.id.et_mountainFloodCount)
    EditText mEtMountainFloodCount;
    @BindView(R.id.et_debrisFlowCount)
    EditText mEtDebrisFlowCount;
    @BindView(R.id.et_landslideCount)
    EditText mEtLandslideCount;
    @BindView(R.id.et_totalSta)
    EditText mEtTotalSta;
    @BindView(R.id.et_commanderCount)
    EditText mEtCommanderCount;
    @BindView(R.id.et_carCount)
    EditText mEtCarCount;
    @BindView(R.id.et_boatCount)
    EditText mEtBoatCount;
    @BindView(R.id.et_rescuePeopleCount)
    EditText mEtRescuePeopleCount;
    @BindView(R.id.et_evacuatePeopleCount)
    EditText mEtEvacuatePeopleCount;


    private boolean editEnable = true;
    private TyphoonInfoResult.DataBean actionInfo;
    private TyphoonReportResult.DataBean.SubSumBean subSum;
    /**
     * 从已上报列表跳转过来的
     */
    private TyphoonReportResult.DataBean.ReportsBean reportsInfo;


    public static void goActivity(Activity activity, TyphoonInfoResult.DataBean actionInfo, TyphoonReportResult.DataBean.SubSumBean subSum) {
        Intent intent = new Intent(activity, TyphoonFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        intent.putExtra("subSum", subSum);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, TyphoonReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, TyphoonFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_typhoon_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((TyphoonInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        subSum = ((TyphoonReportResult.DataBean.SubSumBean) intent.getSerializableExtra("subSum"));

        reportsInfo = ((TyphoonReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));
    }

    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtClearBillboardCount.setText(actionInfo.getClearBillboardCount());
            mEtTrafficAccidentCount.setText(actionInfo.getTrafficAccidentCount());
            mEtClearCollapseCount.setText(actionInfo.getClearCollapseCount());
            mEtHomeTrappedCount.setText(actionInfo.getHomeTrappedCount());
            mEtBuildCollapseCount.setText(actionInfo.getBuildCollapseCount());
            mEtDrainageCount.setText(actionInfo.getDrainageCount());

            mEtMountainFloodCount.setText(actionInfo.getMountainFloodCount());
            mEtDebrisFlowCount.setText(actionInfo.getDebrisFlowCount());
            mEtLandslideCount.setText(actionInfo.getLandslideCount());
            mEtTotalSta.setText(actionInfo.getTotalCount());
            mEtCommanderCount.setText(actionInfo.getCommanderCount());
            mEtCarCount.setText(actionInfo.getCarCount());
            mEtBoatCount.setText(actionInfo.getBoatCount());
            mEtRescuePeopleCount.setText(actionInfo.getRescuePeopleCount());
            mEtEvacuatePeopleCount.setText(actionInfo.getEvacuatePeopleCount());
        } else {
            if (subSum != null) {
                mEtClearBillboardCount.setText(subSum.getClearBillboardCount());
                mEtTrafficAccidentCount.setText(subSum.getTrafficAccidentCount());
                mEtClearCollapseCount.setText(subSum.getClearCollapseCount());
                mEtHomeTrappedCount.setText(subSum.getHomeTrappedCount());
                mEtBuildCollapseCount.setText(subSum.getBuildCollapseCount());
                mEtDrainageCount.setText(subSum.getDrainageCount());

                mEtMountainFloodCount.setText(subSum.getMountainFloodCount());
                mEtDebrisFlowCount.setText(subSum.getDebrisFlowCount());
                mEtLandslideCount.setText(subSum.getLandslideCount());
                mEtTotalSta.setText(subSum.getTotalCount());
                mEtCommanderCount.setText(subSum.getCommanderCount());
                mEtCarCount.setText(subSum.getCarCount());
                mEtBoatCount.setText(subSum.getBoatCount());
                mEtRescuePeopleCount.setText(subSum.getRescuePeopleCount());
                mEtEvacuatePeopleCount.setText(subSum.getEvacuatePeopleCount());
            }
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtClearBillboardCount.setText(reportsInfo.getClearBillboardCount());
            mEtTrafficAccidentCount.setText(reportsInfo.getTrafficAccidentCount());
            mEtClearCollapseCount.setText(reportsInfo.getClearCollapseCount());
            mEtHomeTrappedCount.setText(reportsInfo.getHomeTrappedCount());
            mEtBuildCollapseCount.setText(reportsInfo.getBuildCollapseCount());
            mEtDrainageCount.setText(reportsInfo.getDrainageCount());

            mEtMountainFloodCount.setText(reportsInfo.getMountainFloodCount());
            mEtDebrisFlowCount.setText(reportsInfo.getDebrisFlowCount());
            mEtLandslideCount.setText(reportsInfo.getLandslideCount());
            mEtTotalSta.setText(reportsInfo.getTotalCount());
            mEtCommanderCount.setText(reportsInfo.getCommanderCount());
            mEtCarCount.setText(reportsInfo.getCarCount());
            mEtBoatCount.setText(reportsInfo.getBoatCount());
            mEtRescuePeopleCount.setText(reportsInfo.getRescuePeopleCount());
            mEtEvacuatePeopleCount.setText(reportsInfo.getEvacuatePeopleCount());

            mEtClearBillboardCount.setEnabled(false);
            mEtTrafficAccidentCount.setEnabled(false);
            mEtClearCollapseCount.setEnabled(false);
            mEtHomeTrappedCount.setEnabled(false);
            mEtBuildCollapseCount.setEnabled(false);
            mEtDrainageCount.setEnabled(false);

            mEtMountainFloodCount.setEnabled(false);
            mEtDebrisFlowCount.setEnabled(false);
            mEtLandslideCount.setEnabled(false);
            mEtTotalSta.setEnabled(false);
            mEtCommanderCount.setEnabled(false);
            mEtCarCount.setEnabled(false);
            mEtBoatCount.setEnabled(false);
            mEtRescuePeopleCount.setEnabled(false);
            mEtEvacuatePeopleCount.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }

        TextWatcher countWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    int clearBillboardCount = Integer.parseInt(mEtClearBillboardCount.getText().toString());
                    int trafficAccidentCount = Integer.parseInt(mEtTrafficAccidentCount.getText().toString());
                    int clearCollapseCount = Integer.parseInt(mEtClearCollapseCount.getText().toString());
                    int homeTrappedCount = Integer.parseInt(mEtHomeTrappedCount.getText().toString());
                    int buildCollapseCount = Integer.parseInt(mEtBuildCollapseCount.getText().toString());
                    int drainageCount = Integer.parseInt(mEtDrainageCount.getText().toString());
                    int mountainFloodCount = Integer.parseInt(mEtMountainFloodCount.getText().toString());
                    int debrisFlowCount = Integer.parseInt(mEtDebrisFlowCount.getText().toString());
                    int landslideCount = Integer.parseInt(mEtLandslideCount.getText().toString());
                    mEtTotalSta.setText(clearBillboardCount + trafficAccidentCount + clearCollapseCount +
                            homeTrappedCount + buildCollapseCount + drainageCount + mountainFloodCount +
                            debrisFlowCount + landslideCount + "");
                } catch (Exception e) {
                    mEtTotalSta.setText("NaN");
                }
            }
        };

        mEtClearBillboardCount.addTextChangedListener(countWatcher);
        mEtTrafficAccidentCount.addTextChangedListener(countWatcher);
        mEtClearCollapseCount.addTextChangedListener(countWatcher);
        mEtHomeTrappedCount.addTextChangedListener(countWatcher);
        mEtBuildCollapseCount.addTextChangedListener(countWatcher);
        mEtDrainageCount.addTextChangedListener(countWatcher);
        mEtMountainFloodCount.addTextChangedListener(countWatcher);
        mEtDebrisFlowCount.addTextChangedListener(countWatcher);
        mEtLandslideCount.addTextChangedListener(countWatcher);
    }

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (actionInfo == null) {
                    actionInfo = new TyphoonInfoResult.DataBean();
                    setData();
                    saveReportInfo(actionInfo);
                } else {
                    setData();
                    updateReportInfo(actionInfo);
                }
                break;
        }
    }

    private void setData() {
        actionInfo.setClearBillboardCount(TextUtils.isEmpty(mEtClearBillboardCount.getText().toString()) ? "0" : mEtClearBillboardCount.getText().toString());
        actionInfo.setTrafficAccidentCount(TextUtils.isEmpty(mEtTrafficAccidentCount.getText().toString()) ? "0" : mEtTrafficAccidentCount.getText().toString());
        actionInfo.setClearCollapseCount(TextUtils.isEmpty(mEtClearCollapseCount.getText().toString()) ? "0" : mEtClearCollapseCount.getText().toString());
        actionInfo.setHomeTrappedCount(TextUtils.isEmpty(mEtHomeTrappedCount.getText().toString()) ? "0" : mEtHomeTrappedCount.getText().toString());
        actionInfo.setBuildCollapseCount(TextUtils.isEmpty(mEtBuildCollapseCount.getText().toString()) ? "0" : mEtBuildCollapseCount.getText().toString());
        actionInfo.setDrainageCount(TextUtils.isEmpty(mEtDrainageCount.getText().toString()) ? "0" : mEtDrainageCount.getText().toString());

        actionInfo.setMountainFloodCount(TextUtils.isEmpty(mEtMountainFloodCount.getText().toString()) ? "0" : mEtMountainFloodCount.getText().toString());
        actionInfo.setDebrisFlowCount(TextUtils.isEmpty(mEtDebrisFlowCount.getText().toString()) ? "0" : mEtDebrisFlowCount.getText().toString());
        actionInfo.setLandslideCount(TextUtils.isEmpty(mEtLandslideCount.getText().toString()) ? "0" : mEtLandslideCount.getText().toString());
        actionInfo.setTotalCount(TextUtils.isEmpty(mEtTotalSta.getText().toString()) ? "0" : mEtTotalSta.getText().toString());
        actionInfo.setCommanderCount(TextUtils.isEmpty(mEtCommanderCount.getText().toString()) ? "0" : mEtCommanderCount.getText().toString());
        actionInfo.setCarCount(TextUtils.isEmpty(mEtCarCount.getText().toString()) ? "0" : mEtCarCount.getText().toString());
        actionInfo.setBoatCount(TextUtils.isEmpty(mEtBoatCount.getText().toString()) ? "0" : mEtBoatCount.getText().toString());
        actionInfo.setRescuePeopleCount(TextUtils.isEmpty(mEtRescuePeopleCount.getText().toString()) ? "0" : mEtRescuePeopleCount.getText().toString());
        actionInfo.setEvacuatePeopleCount(TextUtils.isEmpty(mEtEvacuatePeopleCount.getText().toString()) ? "0" : mEtEvacuatePeopleCount.getText().toString());
    }

    private void updateReportInfo(TyphoonInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateTyphoonReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveReportInfo(TyphoonInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveTyphoonReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }
}
